package com.pan.admin.module.api.controller;

import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.module.api.entity.ApiKey;
import com.pan.admin.module.api.entity.vo.ApiKeyBalanceOperationVO;
import com.pan.admin.module.api.entity.vo.ReqApiKeyVO;
import com.pan.admin.module.api.service.ApiKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * keysController
 *
 * @author ts
 * @date 2023-10-26
 */
@RestController
@RequestMapping("/api/keys")
public class ApiKeyController extends BaseController {

    @Autowired
    private ApiKeyService apiKeysService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('api:keys:list')")
    @GetMapping("/list")
    public TableDataInfo list(ApiKey apiKeys) {
        startPage();
        List<ApiKey> list = apiKeysService.list(apiKeys);
        return getDataTable(list);
    }

    /**
     * 查重
     */
    @PreAuthorize("@ss.hasPermi('api:keys:add')")
    @GetMapping("/check")
    public AjaxResult checkApiName(ApiKey apiKeys) {
        boolean flag = apiKeysService.checkApiName(apiKeys);
        return AjaxResult.success(flag);
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:keys:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(apiKeysService.getById(id));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('api:keys:add')")
    @Log(title = "API渠道新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReqApiKeyVO reqApiKeyVO) {
        return toAjax(apiKeysService.insertApiKeys(reqApiKeyVO));
    }

    /**
     * 复制
     */
    @PreAuthorize("@ss.hasPermi('api:keys:copy')")
    @Log(title = "API渠道复制", businessType = BusinessType.INSERT)
    @PostMapping("copy")
    public AjaxResult copy(@RequestBody ApiKey ApiKey) {
        return success(apiKeysService.copyApiKeys(ApiKey));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('api:keys:edit')")
    @Log(title = "API渠道修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReqApiKeyVO reqApiKeyVO) {
        return toAjax(apiKeysService.updateApiKeys(reqApiKeyVO));
    }

    /**
     * 重置密钥
     */
    @PreAuthorize("@ss.hasPermi('api:keys:edit')")
    @Log(title = "API重置密钥", businessType = BusinessType.UPDATE)
    @PutMapping("/reset")
    public AjaxResult reset(@RequestBody ApiKey apiKeys) {
        return toAjax(apiKeysService.resetApiKeys(apiKeys));
    }

    /**
     * 充值或扣款
     */
    @PreAuthorize("@ss.hasPermi('api:keys:amount')")
    @Log(title = "操作渠道金额", businessType = BusinessType.UPDATE)
    @PostMapping("/recharge")
    public AjaxResult balanceOperation(@RequestBody ApiKeyBalanceOperationVO balanceOperationVO) {
        apiKeysService.balanceOperation(balanceOperationVO);
        return toAjax(true);
    }

}
